package com.android.djlmapp.activity.kuandai;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.android.djlmapp.R;
import com.android.djlmapp.adapter.kuandai.KuanDaiSimNumAdapter;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.model.kaika.SimNumInfo;
import com.android.djlmapp.tools.global.ToastUtils;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.refresh_view.pullrecyclerview.PullRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 宽带SIM卡号
 */
public class KuanDaiSimNumActivity extends Activity {

    @BindView(R.id.sim_search)
    EditText simSearch;
    @BindView(R.id.rl_progress)
    RelativeLayout rlProgress;
    @BindView(R.id.kuandai_sim_recycler)
    PullRecyclerView kuandaiSimRecycler;

    private KuanDaiSimNumAdapter kuanDaiSimNumAdapter;
    private String isBaiKa = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kuan_dai_sim_num);
        ButterKnife.bind(this);
        initView();
    }

    protected void initView() {
        isBaiKa = getIntent().getStringExtra("isBaiKa");
        simSearch.addTextChangedListener(phoneTextW);  //监听输入框变化
        initData();
    }

    protected void initData() {
        kuanDaiSimNumAdapter = new KuanDaiSimNumAdapter(KuanDaiSimNumActivity.this);
        kuandaiSimRecycler.setAdapter(kuanDaiSimNumAdapter);
        kuandaiSimRecycler.enablePullRefresh(false);
        kuandaiSimRecycler.enableLoadMore(false); // 禁止加载
        kuanDaiSimNumAdapter.setOnItemClickListener(new KuanDaiSimNumAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(SimNumInfo.IccidListBean item) {
                // 保存数据,返回界面
                Intent intent = new Intent();
                intent.putExtra("SIM_NUM", item.getNum()); // 获取SIM卡号
                setResult(RESULT_OK, intent);
                finish();
                overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);

            }
        });
        getSimData("");
    }

    /**
     * 获取SIM号码
     *
     * @param searchValue
     */
    private void getSimData(String searchValue) {
        rlProgress.setVisibility(View.VISIBLE);
        MyApplication.getInstance().getHttpTools().getSimNum(isBaiKa,"", "", searchValue,
                "", new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            parseJson(response.body());
                        } catch (Exception e) {
                            e.printStackTrace();
                            ToastUtils.showShortToast(KuanDaiSimNumActivity.this, "数据解析异常");
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.showShortToast(KuanDaiSimNumActivity.this, "网络或服务器错误");
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        if (kuandaiSimRecycler != null) {
                            kuandaiSimRecycler.postDelayed(() -> {
                                kuandaiSimRecycler.stopLoadMore();
                                kuandaiSimRecycler.stopRefresh();
                            }, 300);
                        }
                        rlProgress.setVisibility(View.GONE);
                    }
                });
    }

    /**
     * 解析数据
     *
     * @param json
     */
    private void parseJson(String json) {
        Log.e("项目异常智慧沃家", "------parseSimNumJson------" + json);
        Gson gson = new Gson();
        SimNumInfo simNumInfo = gson.fromJson(json, SimNumInfo.class);
        if (simNumInfo != null) {
            int code = simNumInfo.getCode();
            if (code == 300) {
                kuanDaiSimNumAdapter.clear(); // 每次输入的时候，重新初始化数据列表
                List<SimNumInfo.IccidListBean> iccidList = simNumInfo.getIccid_list(); // SIM卡数据
                if (iccidList != null && iccidList.size() > 0) {

                    kuanDaiSimNumAdapter.addAll(iccidList);
                } else {
                    ToastUtils.showShortToast(KuanDaiSimNumActivity.this, "未检测到可用SIM卡");
                }
            } else {

                ToastUtils.showShortToast(KuanDaiSimNumActivity.this,
                        simNumInfo.getMsg());
            }
        } else {
            ToastUtils.showShortToast(KuanDaiSimNumActivity.this, "未获取到数据");
        }
    }

    /**
     * 监听输入框变化
     */
    TextWatcher phoneTextW = new TextWatcher() { // EditText变化监听

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            Log.e("项目异常simNum", "------afterTextChanged------" + s.toString());

            String searchValue = simSearch.getText().toString();
            //将光标移至文字末尾
            simSearch.setSelection(searchValue.length());
            if (!TextUtils.isEmpty(searchValue)) {
                getSimData(searchValue);
            } else {
                kuanDaiSimNumAdapter.clear();
            }
        }
    };

}
